//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/8.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
       
        let account = UserAccount.loadAccount()
        print(account)

        window = UIWindow(frame: UIScreen .mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        //查看版本
       // print(isNewVersion())
        
        //注册通知
        
        
        //设置主题色
        setThemColor()
        
        //根视图控制器
        
        window?.rootViewController = PictureSelectorViewController()
        
        registerNotification()

        return true
    }

    private func setThemColor() {
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
    //MARK: 检测新版本

    private func isNewVersion() -> Bool{
        
        //比较新旧版本
        //1.获取新版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionNum = Double(versionStr)
        //2.获取 旧版本  一般存放在沙盒3
        
        let key = "sandBoxKey"
        let userdefaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = userdefaults.doubleForKey(key)
        print(lastVersion)
        
        //3.保存新版本
        userdefaults.setDouble(versionNum!, forKey: key)
        //同步沙盒
        userdefaults.synchronize()
        
        //4.比较新旧版本
        return versionNum! > lastVersion
    }

    //MARK: 监听通知
    private func registerNotification() {
        //object  观测某个对象发出的通知 如果为nil  就是观测所有的对象发出的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToRootVC:", name: AppSwitchRootViewControler, object: nil)
    }
    
    @objc private func switchToRootVC(n: NSNotification) {
       // print(n)
        //跳转到MainVC
        if (n.object as? String ) != nil{
            window?.rootViewController = MainViewController()
            return
        }
        window?.rootViewController = MainViewController()
    }
    
    //MARK: 移除通知  编码规范而已 没有任何作用
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //MARK: 判断用户是否登陆 并且是否是更新版本
    private func defaultRootController() -> UIViewController {
        if UserAccountViewModel().userLogin {
            
        return isNewVersion() ? NewFeatureViewController() : WelComeViewControl()
        }
        return MainViewController()
    }
   
    
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

